add fast path for compute bounding sphere/box from attribute data for common case (~2.5x speedup) - #34204
add fast path for compute bounding sphere/box from attribute data for common case (~2.5x speedup)#34204bhouston wants to merge 1 commit into
Conversation
…ved, none 16 bit vertices.
📦 Bundle sizeFull ESM build, minified and gzipped.
🌳 Bundle size after tree-shakingMinimal build including a renderer, camera, empty scene, and dependencies.
|
| const array = position.array; | ||
|
|
||
| _vector.fromBufferAttribute( position, i ); | ||
| if ( position.isInterleavedBufferAttribute !== true && position.normalized === false && |
There was a problem hiding this comment.
How about we create a new method on buffer attribute level called supportsDirectAccess()?
This method would decide on attribute level if a direct access is allowed. The policy would be "non-normalized, non-interleaved, float storage".
So BufferAttribute should check:
supportsDirectAccess() {
const array = this.array;
return this.normalized === false &&
( array instanceof Float32Array || array instanceof Float64Array ||
( typeof Float16Array !== 'undefined' && array instanceof Float16Array ) );
}InterleavedBufferAttribute and GLBufferAttribute both return false.
| @@ -0,0 +1,131 @@ | |||
| /** | |||
| * Benchmark: BufferGeometry.computeBoundingSphere() fast path vs the previous | |||
There was a problem hiding this comment.
Actually, we have removed the earlier benchmark suite on purpose. They were not in used because developers often applied benchmarks via jsbench or jsfiddle which are easier to share and execute on various systems like mobile devices.
I would prefer not to adding test/benchmark back.
When profiling the new Gaussian Splatting code I noticed that on Safari, one of the slowest steps was computing the bounding sphere from the BufferGeometry. So I optimized that.
The key insight is that generally BufferGeometry attribute data for positions is not interleaved and it is usually 32 or 64 bit floats. Thus we can avoid accessing the attribute data via its accessors and just access it directly in the majority of cases. This removes indirection on every single data access.
The performance for the fast path is pretty good when the assumptions are meet, otherwise it is identical to before:
I've applied this improvement to both BufferGeometry computeBoundingSphere and the calculation of bounding boxes in Box3. There is a benchmark to confirm this improvement.